package com.sxfq.loansupermarket.backsystem.modular.basicData.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.dao.VipLevelDao;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchVipLevelInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.VipLevelInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IVipLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class VipLevelServiceImpl implements IVipLevelService {

    @Autowired
    private VipLevelDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<VipLevelInfo> selectPageListByQueryGetMap(Page<VipLevelInfo> page, SearchVipLevelInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getVipLevelName())) {
            params.put("vipLevelName", searchInfo.getVipLevelName());
        }
        params.put("tableName", ShiroKit.getUser().getVip_level_info());
        return dao.getQueryPage(page, params);
    }

    public VipLevelInfo selectOneById(String id) {
        String sql = "select * from "+ ShiroKit.getUser().getVip_level_info() +" where id= " + id + "";
        VipLevelInfo info = sqlMapper.selectOne(sql, VipLevelInfo.class);
        return info;
    }

    public int insert(VipLevelInfo info) {
        String sql = "INSERT INTO "+ ShiroKit.getUser().getVip_level_info() +" ( `vip_level_name`, `remarks`, `num`, `integral_factor`) VALUES" +
                "( '" + info.getVipLevelName().trim() + "','" + info.getRemarks().trim() + "'," + info.getNum()+ "" +
                "," + info.getIntegralFactor()+ ");";
        return sqlMapper.insert(sql);
    }

    @Transactional
    public void delete(String id) {
        String sql = "delete from "+ ShiroKit.getUser().getVip_level_info() +" where id = '" + id + "'";
        sqlMapper.delete(sql);
    }

    public int update(VipLevelInfo info) {
        return dao.updateById(info);
    }

    public List<Map<String,Object>>  list(){
        String sql = "select id, vip_level_name, integral_factor from "+ ShiroKit.getUser().getVip_level_info() +" order by num asc;";
        return sqlMapper.selectList(sql);
    }
}